/**
 * 路由生成工具
 * 根据菜单数据自动生成路由配置
 */

/**
 * 将菜单数据转换为路由配置
 * @param {Array} menus 菜单数据
 * @returns {Array} 路由配置
 */
export function generateRoutes(menus) {
    const routes = []
    
    menus.forEach(menu => {
        if (menu.type === 2 && menu.status === 1 && menu.visible === 1) {
            const route = {
                path: menu.path,
                name: menu.name,
                component: menu.component ? () => import(`@/views/${menu.component}.vue`) : null,
                meta: {
                    title: menu.name,
                    icon: menu.icon,
                    permission: menu.permission,
                    sort: menu.sort
                }
            }
            
            // 如果有子菜单，递归处理
            if (menu.children && menu.children.length > 0) {
                route.children = generateRoutes(menu.children)
            }
            
            routes.push(route)
        }
    })
    
    return routes
}

/**
 * 生成路由配置字符串
 * @param {Array} routes 路由配置
 * @returns {String} 路由配置字符串
 */
export function generateRouteConfig(routes) {
    let config = 'import { createRouter, createWebHistory } from \'vue-router\'\n\n'
    config += 'const router = createRouter({\n'
    config += '  history: createWebHistory(import.meta.env.BASE_URL),\n'
    config += '  routes: [\n'
    
    routes.forEach(route => {
        config += generateRouteString(route, 4)
    })
    
    config += '  ],\n'
    config += '})\n\n'
    config += 'export default router\n'
    
    return config
}

/**
 * 生成单个路由的字符串
 * @param {Object} route 路由对象
 * @param {Number} indent 缩进空格数
 * @returns {String} 路由字符串
 */
function generateRouteString(route, indent = 2) {
    const spaces = ' '.repeat(indent)
    let routeStr = spaces + '{\n'
    
    routeStr += spaces + '  path: \'' + route.path + '\',\n'
    routeStr += spaces + '  name: \'' + route.name + '\',\n'
    
    if (route.component) {
        routeStr += spaces + '  component: () => import(\'@/views/' + route.component + '.vue\'),\n'
    }
    
    if (route.meta) {
        routeStr += spaces + '  meta: {\n'
        Object.keys(route.meta).forEach(key => {
            const value = route.meta[key]
            if (typeof value === 'string') {
                routeStr += spaces + '    ' + key + ': \'' + value + '\',\n'
            } else {
                routeStr += spaces + '    ' + key + ': ' + value + ',\n'
            }
        })
        routeStr += spaces + '  },\n'
    }
    
    if (route.children && route.children.length > 0) {
        routeStr += spaces + '  children: [\n'
        route.children.forEach(child => {
            routeStr += generateRouteString(child, indent + 4)
        })
        routeStr += spaces + '  ],\n'
    }
    
    routeStr += spaces + '},\n'
    return routeStr
}

/**
 * 生成Vue组件文件内容
 * @param {String} componentName 组件名称
 * @param {String} title 页面标题
 * @returns {String} Vue组件内容
 */
export function generateVueComponent(componentName, title) {
    return `<template>
  <div class="${componentName.toLowerCase()}-page">
    <div class="page-header">
      <h2>${title}</h2>
    </div>
    <div class="page-content">
      <!-- 在这里添加页面内容 -->
      <el-card>
        <template #header>
          <span>${title}</span>
        </template>
        <div>
          <p>这是自动生成的${title}页面，请根据实际需求修改内容。</p>
        </div>
      </el-card>
    </div>
  </div>
</template>

<script setup>
import { ref, onMounted } from 'vue'

// 页面数据
const pageData = ref({})

// 初始化
onMounted(() => {
  console.log('${title}页面已加载')
})

// 在这里添加页面逻辑
</script>

<style scoped>
.${componentName.toLowerCase()}-page {
  padding: 20px;
}

.page-header {
  margin-bottom: 20px;
}

.page-header h2 {
  margin: 0;
  color: #333;
  font-size: 24px;
  font-weight: 600;
}

.page-content {
  background: #fff;
  border-radius: 8px;
}
</style>
`
}

/**
 * 生成菜单配置
 * @param {Array} menus 菜单数据
 * @returns {String} 菜单配置字符串
 */
export function generateMenuConfig(menus) {
    let config = 'export const menuConfig = [\n'
    
    menus.forEach(menu => {
        config += generateMenuString(menu, 2)
    })
    
    config += ']\n'
    return config
}

/**
 * 生成单个菜单的字符串
 * @param {Object} menu 菜单对象
 * @param {Number} indent 缩进空格数
 * @returns {String} 菜单字符串
 */
function generateMenuString(menu, indent = 2) {
    const spaces = ' '.repeat(indent)
    let menuStr = spaces + '{\n'
    
    menuStr += spaces + '  id: ' + menu.id + ',\n'
    menuStr += spaces + '  name: \'' + menu.name + '\',\n'
    menuStr += spaces + '  path: \'' + (menu.path || '') + '\',\n'
    menuStr += spaces + '  component: \'' + (menu.component || '') + '\',\n'
    menuStr += spaces + '  icon: \'' + (menu.icon || '') + '\',\n'
    menuStr += spaces + '  sort: ' + (menu.sort || 0) + ',\n'
    menuStr += spaces + '  type: ' + menu.type + ',\n'
    menuStr += spaces + '  permission: \'' + (menu.permission || '') + '\',\n'
    menuStr += spaces + '  status: ' + menu.status + ',\n'
    menuStr += spaces + '  visible: ' + menu.visible + ',\n'
    
    if (menu.children && menu.children.length > 0) {
        menuStr += spaces + '  children: [\n'
        menu.children.forEach(child => {
            menuStr += generateMenuString(child, indent + 4)
        })
        menuStr += spaces + '  ],\n'
    }
    
    menuStr += spaces + '},\n'
    return menuStr
} 